package com.example.cachedemo;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

public final class PhotoActivity extends Activity {
    private List<PhotoItem> items = new ArrayList<PhotoItem>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_photo);

        initItems();

        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        PhotoAdapter photoAdapter = new PhotoAdapter(items);
        recyclerView.setAdapter(photoAdapter);

        /**
         * 布局设置，可以线性，网格，瀑布流显示
         */
        // 线性布局，和ListView一样
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        // 横向滑动的ListView
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        // 网格布局
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        // 瀑布流
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);

 //       recyclerView.setLayoutManager(gridLayoutManager);
 //       recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);

    }

    private void initItems() {
        for (int i = 0; i < 100; i++) {
            String name = StrConstant.getStringByRandom();
            String url = UrlConstant.getJpg_420_420();
            PhotoItem item = new PhotoItem(name, url);
            items.add(item);
        }
    }
}
